<?php

namespace mybackend\controllers\official;

use Yii;
use yii\data\Pagination;
use mybackend\controllers\BaseController;
use common\services\official\SubscribeService;
use common\entities\official\BespeakEntity;
use common\error\ErrorCode;
use common\entities\myhtsq\ActionCode;
use common\support\CacheManage;
use yii\helpers\Html;

class BespeakController extends BaseController {
    
    public $_subscribeService;
    public $layout = '@app/views/official/layouts/main.php';
    
    public function __construct($id, $module, SubscribeService $SubscribeService, $config = []) {
        $this->_subscribeService = $SubscribeService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules() {
        return [
            'actions' => [ 'GetList'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'GetList' => ActionCode::OFFICIAL_ALL,
                    ]
                ]
            ]
        ];
    }

    public function actionExport()
    {
        $id = $this->getParam('ids');
        $data = array();
        if(!empty($id)) {
            $data = $this->_subscribeService->getBespeakListByIds($id);
        }

        $objPHPExcel = new \PHPExcel();
        //设置当前的sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //设置sheet的name
        $objPHPExcel->getActiveSheet()->setTitle('Simple');

        //设置单元格的值
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '姓名');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', '电话');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', '城市');
        $objPHPExcel->getActiveSheet()->setCellValue('D1', '公司');
        $objPHPExcel->getActiveSheet()->setCellValue('E1', '来源');
        $objPHPExcel->getActiveSheet()->setCellValue('F1', '创建时间');

        if (!empty($data)) {
            $j = 2;
            foreach ($data as $key => $val) {
                $i = 'A';
                $objPHPExcel->getActiveSheet()->setCellValue($i . $j, $val['name']); // 姓名
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue($i . $j, $val['tel']); // 电话
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue($i . $j, $val['city']); //城市
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue($i . $j, $val['company']); //公司
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue($i . $j, $val['source'] == 1 ? "移动" : "PC"); //来源
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue($i . $j, date('Y-m-d H:i:s',($val['created_at']))); //创建时间
                $j++;
            }
        }

        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename="预约用户列表' . date("YmdHis") . '.xls"');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
        exit;
    }
    
    
    public function actionGetList () {
        $data = array();
        if ($this->isAjaxRequest()) {
            if (Yii::$app->request->get('update')) {
                $section  = Yii::$app->request->get('update');
                $value = Yii::$app->request->get('value');
                $ids = Yii::$app->request->get('ids');
                if ( ! empty($section) && ! empty($ids)) {
                    $this->_articleService->updateBatch($section, $value, $ids); // 批量修改
                }
            }
            $page = Yii::$app->request->get('page', 1);
            $page_size = Yii::$app->request->get('rows', 20);
            //$sidx = Yii::$app->request->get('sidx', 'id');
            $sord = Yii::$app->request->get('sord', 'DESC');   
            $search = Yii::$app->request->get('keyword');   
            //$sidx = $this->_articleService->dealField($sidx);

            $count = BespeakEntity::find();
            if ( ! empty($search)) {
                $count = $count->where(['like', 'name', $search])->orwhere(['like', 'tel', $search]);
            }
            $pages = new Pagination(['totalCount' => $count->count()]);
            $pages->setPageSize($page_size, true);            

            $result = $this->_subscribeService->getBespeakList($page, $page_size, $search);
            foreach ($result as $key => $item) {
                $data['records'] = $pages->totalCount;
                $data['total'] = ceil($pages->totalCount / $page_size);
                $data['rows'][$key] = array(
                    'id' => $item->id,
                    'name' => $item->name,
                    'city' => html_entity_decode($item->city),
                    'company' => html_entity_decode($item->company),
                    'tel' => html_entity_decode($item->tel),
                    'created_at' => date('Y-m-d H:i:s', $item->created_at),
                    'source' => $item->source== 1 ? "移动" : "PC"
                );
            }   
            echo json_encode($data);
            return Yii::$app->end();            
        }
        return $this->render('getList');
    }
    
}